package com.example.demo.generator.scaffold.registry;

import com.example.demo.generator.scaffold.model.TemplateContext;
import com.example.demo.generator.scaffold.model.TemplateDescriptor;
import com.example.demo.generator.scaffold.util.ScaffoldNameUtils;

import java.util.List;

/**
 * MyBatis-Plus 后端模板集合，目前仅包含实体模板，后续逐步扩展。
 */
public final class BackendMybatisPlusTemplates {

    private BackendMybatisPlusTemplates() {
    }

    public static List<TemplateDescriptor> all() {
        return List.of(
                entity(),
                controller(),
                service(),
                serviceImpl(),
                mapper(),
                requestDto(),
                responseDto(),
                updateDto(),
                queryDto()
        );
    }

    public static TemplateDescriptor entity() {
        return TemplateDescriptor.builder()
                .id("mybatis-plus-entity")
                .templatePath("scaffold/templates/backend/mybatis-plus/entity.ftl")
                .outputPathResolver(context -> buildOutputPath(context, "model.entity", context.getClassInfo().getClassName() + ".java"))
                .build();
    }

    public static TemplateDescriptor controller() {
        return TemplateDescriptor.builder()
                .id("mybatis-plus-controller")
                .templatePath("scaffold/templates/backend/mybatis-plus/controller.ftl")
                .outputPathResolver(context -> buildOutputPath(context, "controller", context.getClassInfo().getClassName() + "Controller.java"))
                .build();
    }

    public static TemplateDescriptor service() {
        return TemplateDescriptor.builder()
                .id("mybatis-plus-service")
                .templatePath("scaffold/templates/backend/mybatis-plus/service.ftl")
                .outputPathResolver(context -> buildOutputPath(context, "service", context.getClassInfo().getClassName() + "Service.java"))
                .build();
    }

    public static TemplateDescriptor serviceImpl() {
        return TemplateDescriptor.builder()
                .id("mybatis-plus-service-impl")
                .templatePath("scaffold/templates/backend/mybatis-plus/service-impl.ftl")
                .outputPathResolver(context -> buildOutputPath(context, "service/impl", context.getClassInfo().getClassName() + "ServiceImpl.java"))
                .build();
    }

    public static TemplateDescriptor mapper() {
        return TemplateDescriptor.builder()
                .id("mybatis-plus-mapper")
                .templatePath("scaffold/templates/backend/mybatis-plus/mapper.ftl")
                .outputPathResolver(context -> buildOutputPath(context, "mapper", context.getClassInfo().getClassName() + "Mapper.java"))
                .build();
    }

    public static TemplateDescriptor requestDto() {
        return TemplateDescriptor.builder()
                .id("mybatis-plus-request-dto")
                .templatePath("scaffold/templates/backend/mybatis-plus/request-dto.ftl")
                .outputPathResolver(context -> buildOutputPath(context, "model.dto", context.getClassInfo().getClassName() + "CreateRequest.java"))
                .build();
    }

    public static TemplateDescriptor responseDto() {
        return TemplateDescriptor.builder()
                .id("mybatis-plus-response-dto")
                .templatePath("scaffold/templates/backend/mybatis-plus/response-dto.ftl")
                .outputPathResolver(context -> buildOutputPath(context, "model.dto", context.getClassInfo().getClassName() + "Response.java"))
                .build();
    }

    public static TemplateDescriptor updateDto() {
        return TemplateDescriptor.builder()
                .id("mybatis-plus-update-dto")
                .templatePath("scaffold/templates/backend/mybatis-plus/update-dto.ftl")
                .outputPathResolver(context -> buildOutputPath(context, "model.dto", context.getClassInfo().getClassName() + "UpdateRequest.java"))
                .build();
    }

    public static TemplateDescriptor queryDto() {
        return TemplateDescriptor.builder()
                .id("mybatis-plus-query-dto")
                .templatePath("scaffold/templates/backend/mybatis-plus/query-dto.ftl")
                .outputPathResolver(context -> buildOutputPath(context, "model.dto", context.getClassInfo().getClassName() + "QueryRequest.java"))
                .build();
    }

    private static String buildOutputPath(TemplateContext context, String layer, String fileName) {
        String projectRoot = ScaffoldNameUtils.deriveArtifactId(context);
        String moduleRootPath = context.getModulePath();
        String layerPath = layer.replace('.', '/');
        StringBuilder builder = new StringBuilder(projectRoot)
                .append("/src/main/java/")
                .append(moduleRootPath);
        if (!layerPath.isBlank()) {
            builder.append("/").append(layerPath);
        }
        builder.append("/").append(fileName);
        return builder.toString();
    }
}
